<?php

require_once dirname(__FILE__).'/../lib/p1resultGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/p1resultGeneratorHelper.class.php';

/**
 * p1result actions.
 *
 * @package    pronos
 * @subpackage p1result
 * @author     sthiebaut
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class p1resultActions extends autoP1resultActions
{
  public function executeUpdate(sfWebRequest $request)
  {
	$id = $request->getParameter('id');
	$pronos_p1_result = $request->getParameter("pronos_p1_result");
	$score1 = $pronos_p1_result['score1'];
	$score2 = $pronos_p1_result['score2'];
 
    $p1result = Doctrine::getTable('PronosP1Result')
      ->createQuery('a')
	  ->where('id = '.$id)
      ->fetchOne();
     	
	$p1result->setScore1($score1);
	$p1result->setScore2($score2);
	$p1result->save();
		
	$p1match = $p1result->getPronosP1Match();
	$pronosList = $p1match->getP1Pronos();
	
	// update team standings
	$team1 = $p1match->getTeam1();
	$team2 = $p1match->getTeam2();
	
	updateTeamStanding($team1);
	updateTeamStanding($team2);
		
	// update pronostics
	foreach($pronosList as $i => $pronos) {
		$standing = $pronos->getStanding();
		$standing->setIsGood(evaluateIsGood($pronos, $score1, $score2));
		$standing->setIsPerfect(evaluateIsPerfect($pronos, $score1, $score2));
		$pts = evaluateNbPts($pronos, $score1, $score2);
		$standing->setNbPts($pts);
		$this->logMessage('PTS :: '.$pts, 'err');
		$standing->save();
	}
	
	// update user standings
	$users = Doctrine::getTable('UserProfile')
		->createQuery('a')
		->execute();
	
	foreach($users as $user01) 
	{
		$standing = $user01->getStanding();
		
		$pts = 0;
		$nbGood = 0;
		$nbPerfect = 0;
		$pronosList01 = $user01->getPronos();
		
		foreach($pronosList01 as $pronos01) 
		{
			if($pronos01 != "") {
				$pstd = $pronos01->getStanding();
				$pts += $pstd->getNbPts();
				$nbGood += $pstd->getIsGood();
				$nbPerfect += $pstd->getIsPerfect();
			}
		}
		//echo 'user : '.$user01->getUser()->getUsername().' '.count($pronosList01).' '.$pts.'<br/>';
		$standing->setPts($pts);
		$standing->setNbGood($nbGood);
		$standing->setNbPerfect($nbPerfect);
		$standing->save();
	}
		
  	parent::executeUpdate($request);
  }
}

function updateTeamStanding($team) {
	$standing = $team->getStanding();	
	$pts = 0;
	$nbWin = 0;
	$nbNul = 0;
	$nbLost = 0;
	$nbPlayed = 0;
	$nbBP = 0;
	$nbBC = 0;
	foreach($team->getP1Matchs() as $j => $match) {
		$result = $match->getP1Result();
		$pts += $result->getPointsForTeam($team);
		$nbWin += $result->isWonForTeam($team);
		$nbNul += $result->isNulForTeam($team);
		$nbLost += $result->isLostForTeam($team);
		$nbBP += $result->getBPForTeam($team);
		$nbBC += $result->getBCForTeam($team);
		if($result->getScore1() != "") {
			$nbPlayed++;
		}
	}
	$standing->setNbPts($pts);
	$standing->setNbWon($nbWin);
	$standing->setNbNul($nbNul);
	$standing->setNbLost($nbLost);
	$standing->setNbBP($nbBP);
	$standing->setNbBC($nbBC);
	$standing->setNbPlayed($nbPlayed);
	$standing->save();
}

function evaluateNbPts(PronosP1Pronos $pronos, $score1, $score2) 
{
	$p1good = 2;//sfConfig::get('app_p1pronos_good');
	$p1perfect = 1;//sfConfig::get('p1pronos_perfect');
	
	$isGood = evaluateIsGood($pronos, $score1, $score2);
	$isPerfect = evaluateIsPerfect($pronos, $score1, $score2);
	
	$pts = 0;
	if($isGood)
		$pts += $p1good;
	
	if($isPerfect)
		$pts += $p1perfect;
	
	return $pts;
}

function evaluateIsGood(PronosP1Pronos $pronos, $score1, $score2) 
{
	$isGood = 0;
	
	$predictTeam1Win = $pronos->getScore1() > $pronos->getScore2();
	$team1Win = $score1 > $score2;
	
	if($team1Win && $predictTeam1Win) {
		$isGood = true;
	}
	
	$predictTeamNul = $pronos->getScore1() == $pronos->getScore2();
	$teamNul = $score1 == $score2;
	
	if($teamNul && $predictTeamNul) {
		$isGood = true;
	}
	
	$predictTeam2Win = $pronos->getScore1() < $pronos->getScore2();
	$team2Win = $score1 < $score2;

	if($team2Win && $predictTeam2Win) {
		$isGood = true;
	}
	return $isGood;
}

function evaluateIsPerfect(PronosP1Pronos $pronos, $score1, $score2) 
{
	$score1Good = $pronos->getScore1() == $score1;
	$score2Good = $pronos->getScore2() == $score2;
		
	return $score1Good && $score2Good;
}